﻿Public Class TypeMapper
    Public Const TYPE_UNKNOWN As String = "system.object"
    Public Const TYPE_STRING As String = "system.string"
    Public Const TYPE_GUID As String = "system.guid"
    Public Const TYPE_BOOLEAN As String = "system.boolean"
    Public Const TYPE_DOUBLE As String = "system.double"
    Public Const TYPE_INTEGER As String = "system.integer"
    Public Const TYPE_INTEGER32 As String = "system.int32"
    Public Const TYPE_DATE As String = "system.datetime"

    ''' <summary>
    ''' Default type conversions for DataObject properties.
    ''' </summary>
    ''' <param name="value">system type (in lower case)</param>
    ''' <returns>Mapped SQLTypes enumeration</returns>
    ''' <remarks>
    ''' The DataType() attribute should be used to override these mappings.
    ''' </remarks>
    Public Shared Function MapTypeDefault(ByVal value As String) As SQLTypes
        Select Case value
            Case TYPE_BOOLEAN
                Return SQLTypes.BooleanType
            Case TYPE_DATE
                Return SQLTypes.BizBloxDateType
            Case TYPE_DOUBLE
                Return SQLTypes.DoubleType
            Case TYPE_GUID
                Return SQLTypes.GuidType
            Case TYPE_INTEGER
                Return SQLTypes.IntegerType
            Case TYPE_INTEGER32
                Return SQLTypes.IntegerType
            Case TYPE_STRING
                Return SQLTypes.StringType
            Case Else
                Throw New DOException("Unsupported Type Conversion")
        End Select
    End Function


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="srcObject">object being processed</param>
    ''' <param name="targetType">processing target</param>
    ''' <returns>string formatted for SQL insert</returns>
    ''' <remarks></remarks>
    Public Shared Function TransformValue(ByVal srcObject As Object, ByVal targetType As SQLTypes) As String
        Select Case targetType
            Case SQLTypes.GuidType
                Dim xStr As String = CType(srcObject, System.Guid).ToString()
                If xStr = "00000000-0000-0000-0000-000000000000" Then Return Nothing
                Return xStr
            Case SQLTypes.BooleanType
                If CType(srcObject, Boolean) = True Then
                    Return "1"
                Else
                    Return "0"
                End If
            Case SQLTypes.IntegerType
                Return CType(srcObject, Integer).ToString()
            Case SQLTypes.DoubleType
                Return CType(srcObject, Double).ToString()
            Case SQLTypes.BizBloxDateType
                Dim dtStr As String = CType(srcObject, DateTime).ToString("yyyy-MM-dd HH:mm:ssZ")
                If dtStr = "0001-01-01 00:00:00Z" Then Return Nothing
                Return dtStr
            Case SQLTypes.SQLDateType
                Dim dtStr As String = CType(srcObject, DateTime).ToString("yyyy-MM-dd HH:mm:ss")
                Return dtStr
            Case SQLTypes.StringType
                If srcObject Is Nothing Then Return ""
                Return CType(srcObject, String)
            Case Else
                Throw New DOException("Unsupported SQL Type Conversion")
        End Select
    End Function
End Class
